
import re
import json
class Tool:
    remove = re.compile('amp;')
    rmbig = re.compile('_big')
    make_m = re.compile('mode=medium')
    link = '/member_illust.php?mode=medium&illust_id='
    def removebig(self, x):
        x = re.sub(self.rmbig, "", x)
        return x.strip()
    def removesomething(self, x):
        x = re.sub(self.remove, "", x)
        return x.strip()

    def make_big_url(self, x):
        x = re.sub(self.make_m, "mode=manga_big", x)
        return x.strip()

    def Pic_Type(self, real_url):
        p_type = re.search(re.compile('png', re.S), real_url)
        if p_type == None:
            self.pic_type = 'jpg'
            return self.pic_type
        else:
            self.pic_type = 'png'
            return self.pic_type

    def Pic_Style_M(self, soupfile):
        single = re.findall(re.compile('<.*?work\s_work\s".*?href="(.*?)">', re.S), soupfile)
        multiple = re.findall(re.compile('<.*?work\s_work\smultiple\s.*?href="(.*?)">', re.S), soupfile)
        video = re.findall(re.compile('<.*?work\s_work\sugoku-illust\s.*?href="(.*?)">', re.S), soupfile)
        manga = re.findall(re.compile('<.*?work\s_work\smanga\smultiple\s.*?href="(.*?)">', re.S), soupfile)
        return single, multiple, manga, video

    def Pic_Type_Classify(self,soupfile):
        single = re.findall(re.compile('<.*?href="(.*?)".*work\s*_work\s">'), soupfile)
        multiple = re.findall(re.compile('<.*?href="(.*?)".*work\s*multiple\s">'), soupfile)
        manga = re.findall(re.compile('<.*?href="(.*?)".*work\s*manga\smultiple\s">'), soupfile)
        ugoku = re.findall(re.compile('<.*?href="(.*?)".*work\s*ugoku-illust\s">'), soupfile)
        return single, multiple, manga, ugoku

    def Pic_Style_Json(self, soupfile):
        jsonString = re.findall(re.compile("data-items='.*?'", re.S), soupfile)
        jsonString = jsonString[0]
        jsonString = jsonString[12:len(jsonString) - 1]
        jsonObject = json.loads(jsonString)

        single = []
        multiple = []
        manga = []
        ugoku = []

        for i in range(0, len(jsonObject)):
            type = int(jsonObject[i]['illustType'])
            if type == 0 or type == 1:
                if type == 0 :
                    if int(jsonObject[i]['pageCount']) > 1 :
                        multiple.append(self.link + jsonObject[i]['illustId'])
                    else:
                        single.append(self.link + jsonObject[i]['illustId'])
                if type == 1:
                    manga.append(self.link + jsonObject[i]['illustId'])
            else:
                ugoku.append(self.link + jsonObject[i]['illustId'])

        # print single
        # print multiple
        # print manga
        # print ugoku
        # exit()
        return single, multiple, manga, ugoku


    def Get_Post_Key(self,soupfile):
        key = re.findall(re.compile('<input.*?name="post_key".*?>',re.S), soupfile)
        key = re.search(re.compile('\w{32}', re.S), str(key))
        return key.group(0)